#include <cstdio>
#include <queue>
#include <cstring>

using namespace std;

struct Node
{
    int x, y, f, d;
    Node( ) { }
    Node ( int _x, int _y, int _f, int _d )
        : x( _x ), y( _y ), f( _f ), d( _d ) { }
};

bool operator < ( const Node &a, const Node &b )
{
    return a.d > b.d;
}

priority_queue<Node> q;
int n, m, k, key[ 51 ][ 51 ], f[ 4 ][ 2 ] = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
bool visit[ 51 ][ 51 ][ 1 << 5 ];
char a[ 51 ][ 51 ];

void init( )
{
    int i, j, tx, ty;
    scanf("%d%d%d", &n, &m, &k);
    memset( visit, 0, sizeof( visit ) );
    while ( !q.empty( ) ) q.pop( );
    for ( i = 0; i < n; i++ )
    {
        scanf("%s", a[ i ]);
        for ( j = 0; j < m; j++ )
        {
            key[ i ][ j ] = -1;
            if ( a[ i ][ j ] == 'S' )
                q.push( Node( i, j, 0, 0 ) );
        }
    }
    for ( i = 0; i < k; i++ )
    {
        scanf("%d%d", &tx, &ty); tx--; ty--;
        key[ tx ][ ty ] = i;
    }
}

void work( )
{
    int i, tx, ty, tf, td;
    Node p;
    while ( !q.empty( ) )
    {
        p = q.top( );
        q.pop( );
        visit[ p.x ][ p.y ][ p.f ] = 1;
        if ( a[ p.x ][ p.y ] == 'T' )
            break;
        for ( i = 0; i < 4; i++ )
        {
            tx = p.x + f[ i ][ 0 ]; ty = p.y + f[ i ][ 1 ];
            tf = p.f; td = p.d;
            if ( tx < 0 || tx >= n || ty < 0 || ty >= m || a[ tx ][ ty ] == '#' ) continue;
            if ( key[ tx ][ ty ] != -1 )
                tf |= 1 << key[ tx ][ ty ];
            if ( visit[ tx ][ ty ][ tf ] ) continue;
            if ( a[ tx ][ ty ] >= 'A' && a[ tx ][ ty ] <= 'E' )
                if ( ( tf & ( 1 << ( a[ tx ][ ty ] - 'A' ) ) ) == 0 )
                    continue;
            if ( a[ tx ][ ty ] >= '0' && a[ tx ][ ty ] <= '9' ) td += a[ tx ][ ty ] - '0';
            q.push( Node( tx, ty, tf, td ) );
        }
    }
    if ( q.empty( ) )
        puts("-1");
    else
        printf("%d\n", p.d);
}

int main( )
{
    int t;
    scanf("%d", &t);
    while ( t-- )
    {
        init( );
        work( );
    }
    return 0;
}
